
import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class Admin {
    static Scanner input = new Scanner(System.in);
    public static void inout()
    {
        System.out.println("1-入库 2-出库 3-一键入库 0-返回菜单");
        int a = input.nextInt();
        ArrayList<Company> list = new ArrayList<>();
        switch (a) {
            case 1:
                System.out.print("请输入用户号码: ");
                String num = input.next();
                System.out.print("请输入用户名字: ");
                String company = input.next();
                System.out.print("请输入货物编号: ");
                String goodsnum = input.next();
                System.out.print("请输入货物名字: ");
                String goodsname = input.next();
                System.out.print("请输入货物数量: ");
                int amount = input.nextInt();
                System.out.print("时间:年 ");
                int year = input.nextInt();
                System.out.print("时间:月 ");
                int month = input.nextInt();
                System.out.print("时间:日 ");
                int date = input.nextInt();
                try{
                    BufferedReader in;
                    int t=0;
                    String line;
                    while (true) {
                        in = new BufferedReader(
                                new InputStreamReader(
                                        new FileInputStream("Goods.txt")));
                        while ((line = in.readLine()) != null) {
                            String[] a1 = line.split(" ");
                            int s = amount;
                            if (a1[0].equals(goodsnum)) {
                                t = 1;
                                s = Integer.parseInt(a1[2])+amount;
                                Company a2 = new Company(a1[0], a1[1], s);
                                list.add(a2);
                            }else {
                                Company a2 = new Company(a1[0], a1[1], Integer.parseInt(a1[2]));
                                list.add(a2);
                            }
                        }PrintWriter out = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Goods.txt"))));
                        for (int i = 0; i < list.size(); i++) {
                            out.println(list.get(i).num+" "+list.get(i).company + " "+list.get(i).amount);
                        }out.close();
                        if(t==0){PrintWriter out1 = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Goods.txt",true))));
                            out1.println(goodsnum+" "+goodsname+" "+amount);
                            out1.close();}
                        break;
                    }
                    Admin.show();
                    System.out.println("入库成功!");
                    PrintWriter out1 = new PrintWriter(
                            new BufferedWriter(
                                    new OutputStreamWriter(
                                            new FileOutputStream("Sellcompany.txt",true))));
                    out1.println(num+" "+goodsname+" "+company+" "+year+" "+month+" "+date+" "+amount);
                    out1.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case 2:
                System.out.print("请输入用户号码: ");
                num = input.next();
                System.out.print("请输入用户名字: ");
                company = input.next();
                System.out.print("请输入货物编号: ");
                goodsnum = input.next();
                System.out.print("请输入货物名字: ");
                goodsname = input.next();
                System.out.print("请输入货物数量: ");
                amount = input.nextInt();
                System.out.print("时间:年 ");
                year = input.nextInt();
                System.out.print("时间:月 ");
                month = input.nextInt();
                System.out.print("时间:日 ");
                date = input.nextInt();
                try{
                    BufferedReader in;
                    int t=0;
                    String line;
                    while (true) {
                        in = new BufferedReader(
                                new InputStreamReader(
                                        new FileInputStream("Goods.txt")));
                        while ((line = in.readLine()) != null) {
                            String[] a1 = line.split(" ");
                            int s = Integer.parseInt(a1[2]);
                            if (a1[0].equals(goodsnum)) {
                                t = 1;
                                s = Integer.parseInt(a1[2])-amount;
                            }if (s<0){System.out.println("货物数量不足！");return;}
                            if (s>0) {
                                Company a2 = new Company(a1[0], a1[1], s);
                                list.add(a2);
                            }
                        }PrintWriter out = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Goods.txt"))));
                        for (int i = 0; i < list.size(); i++) {
                            out.println(list.get(i).num+" "+list.get(i).company + " "+list.get(i).amount);
                        }out.close();
                        break;
                    }
                    Admin.show();
                    System.out.println("出库成功!");
                    PrintWriter out1 = new PrintWriter(
                            new BufferedWriter(
                                    new OutputStreamWriter(
                                            new FileOutputStream("Buycompany.txt",true))));
                    out1.println(num+" "+goodsname+" "+company+" "+year+" "+month+" "+date+" "+amount);
                    out1.close();
                }catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case 3:
                try{
                    BufferedReader in;
                    String line;String line1;
                    int s;
                    int e=0;
                    
                    while (true) {
                        in = new BufferedReader(
                                new InputStreamReader(
                                        new FileInputStream("Sellcompany.txt")));
                        while ((line = in.readLine()) != null) {
                            e=0;
                            String[] a1 = line.split(" ");
                            s = Integer.parseInt(a1[6]);
                            for (int i = 0; i < list.size(); i++) {
                                if (Objects.equals(list.get(i).company, a1[1])){
                                    e =1;
                                    String x = list.get(i).num,y = a1[1];
                                    Company z = new Company(x,y,s+list.get(i).amount);
                                    list.set(i,z);
                                }
                            }
                            Company a2 = new Company(a1[0], a1[1], s);
                            if (e==0)list.add(a2);
                        }
                        PrintWriter out = new PrintWriter(
                                new BufferedWriter(
                                        new OutputStreamWriter(
                                                new FileOutputStream("Goods.txt"))));
                        for (int i = 0; i < list.size(); i++) {
                            out.println(list.get(i).num+" "+list.get(i).company + " "+list.get(i).amount);
                        }out.close();
                        break;
                    }
                    Admin.show();
                    System.out.println("入库成功!");
                }catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }



    };
    public static void show()
    {
        System.out.println("编号 货名 在仓数量");
        try{
            BufferedReader in;
            String line;String line1;
            int s;
                in = new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream("Goods.txt")));
                while ((line = in.readLine()) != null) {
                    String[] a1 = line.split(" ");
                    System.out.println(a1[0]+" "+a1[1] + " "+a1[2]);
                }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void search()
    {
        String a = "asoul";
        new Company(a,a);Company.search();
    };
    public static void calvoucher()
    {
        System.out.println("1-入库凭证 2-出库凭证");
        int a = input.nextInt();
        if (a==1) {
            System.out.println("用户号 货名 用户名   年 月 日 数量");
            try {
                BufferedReader in;
                String line;
                in = new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream("Sellcompany.txt")));
                while ((line = in.readLine()) != null) {
                    String[] a1 = line.split(" ");
                    System.out.println(a1[0] + " " + a1[1] + " " + a1[2]+ " " + a1[3]+ " " + a1[4]+ " " + a1[5]+ " " + a1[6]);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (a==2) {
            System.out.println("用户号 货名 用户名   年 月 日 数量");
            try {
                BufferedReader in;
                String line;
                in = new BufferedReader(
                        new InputStreamReader(
                                new FileInputStream("Buycompany.txt")));
                while ((line = in.readLine()) != null) {
                    String[] a1 = line.split(" ");
                    System.out.println(a1[0] + " " + a1[1] + " " + a1[2]+ " " + a1[3]+ " " + a1[4]+ " " + a1[5]+ " " + a1[6]);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void statistics()
    {
        System.out.println("1-期间汇总 2-当天汇总");
        int a = input.nextInt();
        if (a==1) {
            System.out.println("1-入库 2-出库");
            a = input.nextInt();
            if (a==1) {
                int year, date, month;
                int aq, az, as;
                int year1, date1, month1;
                System.out.println("请输入起始 年 月 日");
                year = input.nextInt();month = input.nextInt();date = input.nextInt();
                System.out.println("请输入截至 年 月 日");
                year1 = input.nextInt();month1 = input.nextInt();date1 = input.nextInt();
                aq = year + month * 30 + date;
                az = year1 + month1 * 30 + date1;
                System.out.println("用户号 货名 用户名   年 月 日 数量");
                try {
                    BufferedReader in;
                    String line;
                    in = new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("Sellcompany.txt")));
                    while ((line = in.readLine()) != null) {
                        String[] a1 = line.split(" ");
                        as = Integer.parseInt(a1[3]) + Integer.parseInt(a1[4]) * 30 + Integer.parseInt(a1[5]);
                        if (as >= aq && as <= az) {
                            System.out.println(a1[0] + " " + a1[1] + " " + a1[2] + " " + a1[3] + " " + a1[4] + " " + a1[5] + " " + a1[6]);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if (a==2){
                int year, date, month;
                int aq, az, as;
                int year1, date1, month1;
                System.out.println("请输入起始 年 月 日");
                year = input.nextInt();month = input.nextInt();date = input.nextInt();
                System.out.println("请输入截至 年 月 日");
                year1 = input.nextInt();month1 = input.nextInt();date1 = input.nextInt();
                aq = year + month * 30 + date;
                az = year1 + month1 * 30 + date1;
                System.out.println("用户号 货名 用户名   年 月 日 数量");
                try {
                    BufferedReader in;
                    String line;
                    in = new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("Buycompany.txt")));
                    while ((line = in.readLine()) != null) {
                        String[] a1 = line.split(" ");
                        as = Integer.parseInt(a1[3]) + Integer.parseInt(a1[4]) * 30 + Integer.parseInt(a1[5]);
                        if (as >= aq && as <= az) {
                            System.out.println(a1[0] + " " + a1[1] + " " + a1[2] + " " + a1[3] + " " + a1[4] + " " + a1[5] + " " + a1[6]);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (a==2) {
            System.out.println("1-入库 2-出库");
            a = input.nextInt();
            if (a==1) {
                int year, date, month;
                int aq, as;
                System.out.println("请输入 年 月 日");
                year = input.nextInt();month = input.nextInt();date = input.nextInt();
                aq = year + month * 30 + date;

                System.out.println("用户号 货名 用户名   年 月 日 数量");
                try {
                    BufferedReader in;
                    String line;
                    in = new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("Sellcompany.txt")));
                    while ((line = in.readLine()) != null) {
                        String[] a1 = line.split(" ");
                        as = Integer.parseInt(a1[3]) + Integer.parseInt(a1[4]) * 30 + Integer.parseInt(a1[5]);
                        if (as == aq)
                            System.out.println(a1[0] + " " + a1[1] + " " + a1[2] + " " + a1[3] + " " + a1[4] + " " + a1[5] + " " + a1[6]);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (a==2){
                int year, date, month;
                int aq, as;
                System.out.println("请输入 年 月 日");
                year = input.nextInt();month = input.nextInt();date = input.nextInt();
                aq = year + month * 30 + date;

                System.out.println("用户号 货名 用户名   年 月 日 数量");
                try {
                    BufferedReader in;
                    String line;
                    in = new BufferedReader(
                            new InputStreamReader(
                                    new FileInputStream("Buycompany.txt")));
                    while ((line = in.readLine()) != null) {
                        String[] a1 = line.split(" ");
                        as = Integer.parseInt(a1[3]) + Integer.parseInt(a1[4]) * 30 + Integer.parseInt(a1[5]);
                        if (as == aq)
                            System.out.println(a1[0] + " " + a1[1] + " " + a1[2] + " " + a1[3] + " " + a1[4] + " " + a1[5] + " " + a1[6]);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    };

